package HeiMaLesson1;

import com.sun.org.apache.bcel.internal.generic.NEWARRAY;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SwingComponentDemo {
    public static void main(String[] args) {
        JFrame jf = new JFrame("测试swing 基本组件");
        //声明菜单相关的组件
        //声明菜单条
        JMenuBar menuBar= new JMenuBar();
        //声明菜单
        JMenu fileMenu = new JMenu("文件");
        JMenu editMenu = new JMenu("编辑");
        //声明菜单项
        JMenuItem autoEnter = new JMenuItem("自动换行");
        JMenuItem copy = new JMenuItem("复制");
        JMenuItem paste = new JMenuItem("粘贴");
        //声明格式菜单（编辑菜单里面的）
        JMenu formatMenu = new JMenu("格式");
        //声明菜单项（格式菜单里面的）
        JMenuItem comment = new JMenuItem("注释");
        JMenuItem canselComment = new JMenuItem("取消注释");
        //声明文本域
        JTextArea textArea = new JTextArea(8,20);

        //声明列表选择框
        String[] colors={"红色","绿色","蓝色"};
        JList<String> colorList = new JList<>(colors);

        //声明下拉选择框
        JComboBox<String> colorSelect = new JComboBox<>();
        //声明单选按钮组（把相关的单选按钮放到同一个单选按钮组里才能实现多选一）
        ButtonGroup bg = new ButtonGroup();
        //声明单选按钮
        JRadioButton male = new JRadioButton("男",false);
        JRadioButton female = new JRadioButton("女",true);
        //声明复选框
        JCheckBox isMarried = new JCheckBox("是否已婚",true);
        //声明文本框
        JTextField textField= new JTextField(40);
        //声明按钮
        JButton ok = new JButton("确定");
        //声明在文本域中右键菜单
        JPopupMenu jPopupMenu = new JPopupMenu();
        //声明按钮组（用于单选菜单项）
        ButtonGroup poupButtonBg = new ButtonGroup();
        //声明单选菜单项（用于右键菜单）
        JRadioButtonMenuItem metalItem = new JRadioButtonMenuItem("Metal 风格");
        JRadioButtonMenuItem nimbusItem = new JRadioButtonMenuItem("nimbus 风格");
        JRadioButtonMenuItem windowsItem = new JRadioButtonMenuItem("Windows 风格",true);
        JRadioButtonMenuItem windowsClassicItem = new JRadioButtonMenuItem("Windows 经典风格");
        JRadioButtonMenuItem motifItem = new JRadioButtonMenuItem("Motif 风格");

        //初始化界面
            //组装视图
            //组装底部
            JPanel bottomPanel = new JPanel();
            bottomPanel.add(textField);
            bottomPanel.add(ok);

            //把底部放到窗口里
            jf.add(bottomPanel, BorderLayout.SOUTH);

            //组装下拉选择和双选和复选框
            JPanel selectPanel= new JPanel();
            selectPanel.add(colorSelect);
            //先把两个单选按钮放到按钮组里面
            bg.add(male);
            bg.add(female);
            selectPanel.add(male);
            selectPanel.add(female);
            selectPanel.add(isMarried);
            //设置颜色下拉选择框里的选项
            colorSelect.addItem("红色");
            colorSelect.addItem("绿色");
            colorSelect.addItem("蓝色");

            //组装文本域和选择相关的panel
            //因为是上下垂直关系所以创建一个box放
            Box topLeft = Box.createVerticalBox();
            topLeft.add(textArea);
            topLeft.add(selectPanel);

            //组装topleft和右边的列表选择
            Box top=Box.createHorizontalBox();
            top.add(topLeft);
            top.add(colorList);

            //把top放到frame的center区域
            jf.add(top,BorderLayout.CENTER);

            //组装顶部菜单
            //组装格式菜单
            formatMenu.add(comment);
            formatMenu.add(canselComment);
            //组装 编辑 菜单
            editMenu.add(autoEnter);
            editMenu.addSeparator();//加一个分割线
            editMenu.add(copy);
            editMenu.add(paste);
            editMenu.add(formatMenu);

            //将 文件 和 编辑 组装到菜单条
            menuBar.add(fileMenu);
            menuBar.add(editMenu);

            //将菜单条放到窗口中
            jf.setJMenuBar(menuBar);

            //关联右键菜单项
            poupButtonBg.add(metalItem);
            poupButtonBg.add(nimbusItem);
            poupButtonBg.add(windowsItem);
            poupButtonBg.add(windowsClassicItem);
            poupButtonBg.add(motifItem);

            //创建事件监听器
            ActionListener listener = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //用string接收事件
                    String actionCommand=e.getActionCommand();
                    switch (actionCommand){
                        case"Metal 风格":
                            try {
                                UIManager.setLookAndFeel("javax.swing.plaf.metal.metalLookAndFeel");
                            } catch (ClassNotFoundException ex) {
                                throw new RuntimeException(ex);
                            } catch (InstantiationException ex) {
                                throw new RuntimeException(ex);
                            } catch (IllegalAccessException ex) {
                                throw new RuntimeException(ex);
                            } catch (UnsupportedLookAndFeelException ex) {
                                throw new RuntimeException(ex);
                            }
                            break;
                        case"Nimbus 风格":
                            try {
                                UIManager.setLookAndFeel("javax.swing.plaf.nimbus.metalLookAndFeel");
                            } catch (ClassNotFoundException ex) {
                                throw new RuntimeException(ex);
                            } catch (InstantiationException ex) {
                                throw new RuntimeException(ex);
                            } catch (IllegalAccessException ex) {
                                throw new RuntimeException(ex);
                            } catch (UnsupportedLookAndFeelException ex) {
                                throw new RuntimeException(ex);
                            }
                            break;
                    }
                    //改完还需要刷新组件的外观
                    SwingUtilities.updateComponentTreeUI(jf.getContentPane());
                    SwingUtilities.updateComponentTreeUI(menuBar);
                    SwingUtilities.updateComponentTreeUI(jPopupMenu);
                }
            };
//        将创建好的监听器注册给右键菜单的菜单项
            metalItem.addActionListener(listener);
            nimbusItem.addActionListener(listener);
            windowsItem.addActionListener(listener);
            windowsClassicItem.addActionListener(listener);
            motifItem.addActionListener(listener);

            //将菜单项组装到右键菜单
            jPopupMenu.add(metalItem);
            jPopupMenu.add(nimbusItem);
            jPopupMenu.add(windowsItem);
            jPopupMenu.add(windowsClassicItem);
            jPopupMenu.add(motifItem);

            //将右键菜单关联到文本域
            textArea.setComponentPopupMenu(jPopupMenu);

//        设置窗口可见和大小
            jf.setVisible(true);
            jf.pack();
            //设置关闭事件
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}
